package com.ruiysoft.service.order.facade;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.entity.order.OrderMain;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultCode;
import com.ruiysoft.service.order.IOrderMainService;
import com.ruiysoft.service.order.consts.OrderDBStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Date : 2018/11/24
 *
 * @author WenJialu
 */
@Component
public class OrderInnerFacade {

	@Resource
	IOrderMainService orderMainService;
	@Autowired
	OrderPubFacade orderPubFacade;

	public Result<OrderMain> sign4Order(String orderNo) {
		Result<OrderMain> result = new Result<OrderMain>();
		if (null == orderNo) {
			result.setBizCode(ResultCode.CODE_COMMON_ERROR);
			result.setBizMessage("orderNo is null");
			return result;
		}
		OrderMain orderMain = orderPubFacade.getOrderMain(orderNo);
		if(orderMain == null) {
			result.setBizCode(ResultCode.CODE_COMMON_ERROR);
			result.setBizMessage("not exist the order no");
			return result;
		}
		result.setData(orderMain);
		if(!orderMain.getStatus().equals(OrderDBStatusEnum.DELIVERED.getCode())){
			//当前非"已发货"状态
			result.setBizCode(ResultCode.CODE_COMMON_ERROR);
			result.setBizMessage("can't sign delivery");
			return result;
		}
		orderMain.setStatus(OrderDBStatusEnum.SYSTEM_SIGNED.getCode());
		orderMain.setFinalSignTime(new Date());
		boolean update = orderMainService.update(
				orderMain,
				new QueryWrapper<OrderMain>()
						.eq("id",orderMain.getId())
						.eq("order_no", orderNo)
						.eq("status", OrderDBStatusEnum.DELIVERED.getCode())
						.eq("del_flag", false));
		/*if(!update){
			orderMain = orderPubFacade.getOrderMain(orderNo);
		}*/
		if(!update){
			//orderMain = orderPubFacade.getOrderMain(orderNo);
			result.setBizCode(ResultCode.CODE_COMMON_ERROR);
			result.setBizMessage("update data to signed error");
			return result;
		}

		result.setBizCode(ResultCode.CODE_SUCCESS);
		result.setData(orderMain);
		return result;

	}
}
